package com.mediacloud.app.newsmodule.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Binder;
import android.os.IBinder;
import androidx.annotation.Nullable;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.mediacloud.app.assembly.util.BitmapUtil;
import com.mediacloud.app.assembly.util.FunKt;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.newsmodule.service.servicenotification.MediaNotificationManager;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;
import com.tencent.mmkv.MMKV;

/**
 * @author TXL
 * description :将音频直播点播播放服务抽取一个公共类
 */
public abstract class AbsAudioPlayService extends Service {
    protected boolean isPlay=false;
    protected MediaNotificationManager mMediaNotificationManager;

    MyGlideLoadListener glideLoadListener ;

    class MyGlideLoadListener implements RequestListener<Drawable> {

        public MyGlideLoadListener(ArticleItem currentVodAudio) {
            this.currentVodAudio = currentVodAudio;
        }

        boolean discard = false;
        ArticleItem currentVodAudio;
      /*  @Override
        public void failed() {
            if(discard){
                return;
            }
            Bitmap bitmap = BitmapUtil.drawableToBitmap(AppFactoryGlobalConfig.getDefaultImageLoadDrawable());
            Notification notification = mMediaNotificationManager.getNotification(currentVodAudio, isPlay,bitmap);
            startForeground(MediaNotificationManager.NOTIFICATION_ID, notification);
        }*/

/*        @Override
        public void success() {

        }

        @Override
        public void success(Drawable drawable) {
            if(discard){
                return;
            }
            Bitmap bitmap = null;
            if(drawable instanceof BitmapDrawable){
                bitmap = BitmapUtil.drawableToBitmap(drawable);
            }else if(drawable instanceof GifDrawable){
                GifDrawable gifDrawable = (GifDrawable) drawable;
                bitmap = gifDrawable.getFirstFrame();
            }
            Notification notification = mMediaNotificationManager.getNotification(currentVodAudio, isPlay,bitmap);
            startForeground(MediaNotificationManager.NOTIFICATION_ID, notification);
        }*/

        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
            if(discard){
                return false;
            }
            Bitmap bitmap = BitmapUtil.drawableToBitmap(AppFactoryGlobalConfig.getDefaultImageLoadDrawable());
            Notification notification = mMediaNotificationManager.getNotification(currentVodAudio, isPlay,bitmap);
            startForeground(MediaNotificationManager.NOTIFICATION_ID, notification);
            return false;
        }

        @Override
        public boolean onResourceReady(Drawable drawable, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
            if(discard){
                return false;
            }
            Bitmap bitmap = null;
            if(drawable instanceof BitmapDrawable){
                bitmap = BitmapUtil.drawableToBitmap(drawable);
            }else if(drawable instanceof GifDrawable){
                GifDrawable gifDrawable = (GifDrawable) drawable;
                bitmap = gifDrawable.getFirstFrame();
            }
            Notification notification = mMediaNotificationManager.getNotification(currentVodAudio, isPlay,bitmap);
            startForeground(MediaNotificationManager.NOTIFICATION_ID, notification);
            return false;
        }
    }

    protected void startNotification(final ArticleItem currentVodAudio, final boolean isPlay) {
        if(currentVodAudio != null){
            if(glideLoadListener == null){
                glideLoadListener = new MyGlideLoadListener(currentVodAudio);
            }else {
                if(currentVodAudio.getId() != glideLoadListener.currentVodAudio.getId()){
                    glideLoadListener.discard = true;
                    glideLoadListener = new MyGlideLoadListener(currentVodAudio);
                }
            }
            FunKt.otherLoad(this,currentVodAudio.getLogo(),glideLoadListener);
          //  GlideUtils.loadUrl(currentVodAudio.getLogo(), this,glideLoadListener );
        }else {
            Notification notification = mMediaNotificationManager.getNotification(currentVodAudio, isPlay);
            startForeground(MediaNotificationManager.NOTIFICATION_ID, notification);
        }

    }

    protected void clearLastPlayAudioInfo() {
        MMKV.defaultMMKV().encode("Audio_ID",-1L);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new AudioPlayBinder();
    }

    protected void stopUpdateTime()
    {

    }

    public class AudioPlayBinder extends Binder{
        public boolean isPlay(){
            return isPlay;
        }

        public void stopUpdateTimeLabel()
        {
            stopUpdateTime();
        }
    }
}